<?php
/**
 * Nordic Repository
 * 
 * LICENSE
 * 
 * The new BSD license is applied on this source-file. For further
 * information please visit http://license.nordic-dev.de/newbsd.txt
 * or send an email to andre.moelle@gmail.com.
 */

/**
 * Necessary includes.
 */
Zend_Loader::loadClass('Nordic_Security');
Zend_Loader::loadClass('Nordic_Db_Row');

/**
 * Record for an user.
 * 
 * @category   Nordic
 * @package    Nordic_Auth
 * @copyright  2007 Nordic Development
 * @license    http://license.nordic-dev.de/newbsd.txt (New-BSD license)
 * @author     Andre Moelle <andre.moelle@gmail.com>
 * @version    $Id: User.php 3 2007-07-08 09:21:42Z andre.moelle $
 */
class Nordic_Auth_Record_User extends Nordic_Db_Row
{
	/**
	 * Returns the crypted password.
	 * 
	 * @param string $password password to crypt
	 * @return string
	 */
	protected function _crypt ($password)
	{
		return Nordic_Security::cryptString($password, $this->salt);
	}
	
	/**
	 * Changes the password.
	 * 
	 * If the salt was already set, the password is hashed with the
	 * salt.
	 * 
	 * @param string $password new password
	 * @return void
	 */
	public function changePassword ($password)
	{
		if($this->salt != '')
		{
			$password = $this->_crypt($password);
		}
		
		$this->password = $password;
	}
	
	/**
	 * Returns if the given password matches.
	 * 
	 * @param string $password password of the user
	 * @return boolean
	 */
	public function checkPassword ($password)
	{
		return ($this->_crypt($password) == $this->password);
	}
	
	/**
	 * Prepares to validate.
	 * 
	 * If no salt exists, the salt is generated. Additionally the
	 * password is hashed using Nordic_Security::cryptString and
	 * the salt. The correct datetimes are also set.
	 * 
	 * @see Zend_Db_Table_Row::save()
	 */
	protected function _preValidate ()
	{
		if($this->salt == '')
		{
			$this->salt = Nordic_Security::createString();
			$this->password = Nordic_Security::cryptString(
				$this->password,
				$this->salt
			);
			
			$this->register_time = date('Y-m-d H:i:s');
			$this->lasttry = $this->login_time = date('Y-m-d H:i:s', 0);
		}
	}
	
	/**
	 * @see Nordic_Db_Row::validate()
	 */
	public function validate ()
	{
		Zend_Loader::loadClass('Zend_Validate_StringLength');
		Zend_Loader::loadClass('Zend_Validate_Alnum');
		Zend_loader::loadClass('Zend_Validate_EmailAddress');
		
		$chain = new Nordic_Validate_Chain();
		$chain->append(
			new Nordic_Validate_Chain_Element(
				new Zend_Validate_StringLength(3, 32),
				$this->username,
				'Username cannot be shorter than three or longer than 32 characters.'
			),
			new Nordic_Validate_Chain_Element(
				new Zend_Validate_Alnum(),
				$this->username,
				'The username may only contain letters and digits.'
			),
			new Nordic_Validate_Chain_Element(
				new Zend_Validate_EmailAddress(),
				$this->email,
				'Please enter a valid email-address.'
			),
			new Nordic_Validate_Chain_Element(
				new Zend_Validate_StringLength(6, 40),
				$this->password,
				'Your password-length should be between six and 40 characters (six and 40 including).'
			)
		);
		
		if(!$chain->isValid(null))
		{
			$messages = $chain->getMessages();
		}
		else
		{
			$messages = array();
		}
		
		$tmpUser = strtolower($this->username);
		$tmpMail = strtolower($this->email);
		
		$class = $this->getTableClass();
		$model = new $class();
		
		foreach($model->findUniques($this->username, $this->email) as $item)
		{
			if($this->id == $item['id'])
			{
				continue;
			}
			
			if($tmpMail == strtolower($item['email']))
			{
				$messages[] = 'The email-address you have chosen already exists.';
			}
			if($tmpUser == strtolower($item['username']))
			{
				$messages[] = 'Your chosen username is occupied.';
			}
		}
		
		if($messages)
		{
			throw new Zend_Validate_Exception(implode("\n", $messages));
		}
	}
}
?>